package com.vortex.cloud.zhsw.xinyang.enums.ums;

import cn.hutool.core.util.StrUtil;
import com.vortex.cloud.zhsw.xinyang.enums.IBaseEnum;
import com.vortex.cloud.zhsw.xinyang.support.Constants;
import org.springframework.lang.Nullable;

import java.util.Objects;

/**
 * @author bd
 */
public enum DivisionLevelEnum implements IBaseEnum {
    /**
     * 实体名称
     */
    LEVEL_PROVINCE(1, "省"),
    LEVEL_CITY(2, "市"),
    LEVEL_DISTRICT(3, "区/县"),
    LEVEL_VILLAGE(4, "乡镇/街道"),
    LEVEL_RUSTIC(5, "居委会"),
    LEVEL_NATURAL_VILLAGE(6, "自然村");
    private final Integer key;
    private final String value;

    DivisionLevelEnum(Integer key, String value) {
        this.key = key;
        this.value = value;
    }

    @Override
    public int getKey() {
        return key;
    }

    @Override
    public String getValue() {
        return value;
    }

    @Nullable
    public static DivisionLevelEnum getByKey(@Nullable Integer key) {
        if (Objects.nonNull(key)) {
            for (DivisionLevelEnum item : values()) {
                if (key.equals(item.getKey())) {
                    return item;
                }
            }
        }
        return null;
    }

    @Nullable
    public static String getValueByKey(@Nullable Integer key) {
        DivisionLevelEnum divisionLevelEnum = getByKey(key);
        return divisionLevelEnum == null ? null : divisionLevelEnum.getValue();
    }

    @Nullable
    public static Integer getKeyByValue(@Nullable String value) {
        if (StrUtil.isNotBlank(value)) {
            for (DivisionLevelEnum item : values()) {
                if (StrUtil.equalsIgnoreCase(value, item.getValue())) {
                    return item.getKey();
                }
            }
        }
        return null;
    }

    public DivisionLevelEnum getHigherLevel() {
        Integer thisLevel = this.getKey();
        if (thisLevel <= 1) {
            return null;
        }
        return DivisionLevelEnum.getByKey(thisLevel - 1);
    }

    public DivisionLevelEnum getLowerLevel() {
        Integer thisLevel = this.getKey();
        if (thisLevel >= Constants.Figure.FIVE) {
            return null;
        }
        return DivisionLevelEnum.getByKey(thisLevel + 1);
    }
}
